<?php
require_once("inc.admin.php");

set_time_limit(0);

$this_title="$vars[admin_title] &raquo; ".__("Commission Report");
$page_title=__("Commission Report");

if(!$pv["task"]["Commission Report"]){
	$errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to access Commission Report."));

	print format_admin_page($errmsg, $this_title);
	exit();
}

$country_code=$vars['common']['country_code'];
array_shift($country_code);
$country_code_t=$vars['common']['country_code_text'];
foreach($country_code_t as $code=>$cname){
 $country_code_d[$code]=__($cname);
}


if($get_h['date_from']=="" && $get_h['date_to']==""){
	$comm_date = 'all';
}else{
	if($get_h['date_from']<>""){
		$comm_date = "'".$get_h['date_from']. " 00:00:00'";
		
		if($get_h['date_to']<>""){
			$comm_date .= " and '".$get_h['date_to']. " 23:59:59'";
		}else{
			$comm_date .= " and '2020-12-31 23:59:59'";
		}
	}else{
		$comm_date = "'2013-01-01 00:00:00' and '".$get_h['date_to']. " 23:59:59'";
	}
		
		
}

if($get_h['ep_comm']=="1" && $get_h['date_from']==$get_h['date_to']){
	$cdate = date('Y-m-d H:i:s');
	$t=@mysql_num_rows($r=mysql_query("select * from gg_member_comm where status<>'confirmed' and bonus_date='".$get_h['date_to']."' order by type desc"));
	for($i=0;$i<$t;$i++){
		$comm=mysql_fetch_assoc($r);
		
		if($comm['type']=='s'){
			$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$comm['uid']), 0) or 0;			
			$balance+=$comm['amount'];
			mysql_query("update gg_users set ewallet=".$balance." where id=".$comm['uid']);			
			$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$comm['uid'].",type='credit',amount=".$comm['amount'].",bal=".$balance.",descr='sponsor from id ".$comm['from_uid']."',cdate='".$cdate."'";
			mysql_query($sql);
		}
		
		if($comm['type']=='p'){
			$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$comm['uid']), 0) or 0;			
			$balance+=$comm['amount'];
			mysql_query("update gg_users set ewallet=".$balance." where id=".$comm['uid']);			
			$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$comm['uid'].",type='credit',amount=".$comm['amount'].",bal=".$balance.",descr='placement from level ".$comm['level']."',cdate='".$cdate."'";
			mysql_query($sql);
		}
		
		if($comm['type']=='l'){
			$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$comm['uid']), 0) or 0;			
			$balance+=$comm['amount'];
			mysql_query("update gg_users set ewallet=".$balance." where id=".$comm['uid']);			
			$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$comm['uid'].",type='credit',amount=".$comm['amount'].",bal=".$balance.",descr='leadership from id ".$comm['from_uid']."',cdate='".$cdate."'";
			mysql_query($sql);
		}
	}
	
	mysql_query("update gg_member_comm set status='confirmed' where bonus_date='".$get_h['date_to']."'");
}

//#####PRINT BONUS REPORT#####
if($_GET['print_comm']){
	$print_result = print_bonus_summary($year, $month, $get_s['uid']);
	
	print $print_result['header'].$print_result['bonus_summary'];
	exit;
}

if($_GET['__req'] && $comm_date<>'all'){
	
	$use_summary = true;	
	$k=0;
	$count=0;
	if($use_summary){
		$t=@mysql_num_rows($r=mysql_query($s="select sum(c.pgs) as pgs, sum(c.s) as s, sum(c.p) as p, sum(c.l) as l, sum(c.w) as w, u.name, u.bank_name, u.bank_acc_no, u.country, u.pool_share, c.uid from $db->member_comm_sum c, $db->users u where c.uid=u.id and c.bonus_date between ".$comm_date." and c.flag=0 group by c.uid"));				
		for($i=0;$i<$t;$i++){			
			$comm=mysql_fetch_assoc($r);				
			//$group_sales_network = @mysql_result(mysql_query("select sum(a.bv) from $db->sales_bv a inner join $db->purchase b on b.id=a.slid and date_format(b.cdate,'%Y-%m-%d') between '".$calc_date['start']."' and '".$calc_date['end']."'"), 0) or 0;				
			$group_sales_network = $comm['pgs'];		
			$s_comm = $comm['s'];				
			$p_comm = $comm['p'];				
			$l_comm = $comm['l'];			
			$w_comm = $comm['w'];
			$share = $comm['pool_share'];					
			$total=$s_comm+$p_comm+$l_comm+$w_comm;				
			$s_comm_total+=$s_comm;				
			$p_comm_total+=$p_comm;				
			$l_comm_total+=$l_comm;				
			$w_comm_total+=$w_comm;			
			$grand_total+=$total;
			$total_share+=$share;
			$group_sales_network_total+=$group_sales_network;	
			$shares_total+=$comm['pool_share'];	
			
			//if($total+$group_sales_network<=0) continue;
			
			$comm_row.="<tr class='row$k'><td>".($count+1)."</td><td>$comm[uid]</td><td>$comm[name]</td><td>".$country_code_d[$comm['country']]."</td><td>$comm[pool_share]</td><td>".number_format($group_sales_network, 0)."</td><td>".number_format($s_comm, 0)."</td><td>".number_format($p_comm, 0)."</td><td>".number_format($l_comm, 0)."</td><td>".number_format($w_comm, 0)."</td><td>".number_format($total, 0)."</td></tr>";			
			$k=1-$k;
			$count++;	
		}
	}
	
	//total sales
	//$total_volume=@mysql_result(mysql_query("select sum(a.bv) from $db->sales_bv a inner join $db->purchase b on b.id=a.slid and date_format(b.cdate,'%Y-%m-%d') between '".$calc_date['start']."' and '".$calc_date['end']."'"), 0) or 0;
	$total_volume=@mysql_result(mysql_query("select sum(c.pgs) from $db->member_comm_sum c, $db->users u where c.uid=u.id and u.id=10001 and c.bonus_date between ".$comm_date." group by c.uid"), 0) or 0;
	if($total_volume>0)
		$projected_payout=round($total_volume * 70 / 100, 0);
	else
		$projected_payout=0;
	
	$actual_payout_dis=$grand_total_no_pool+$pool_payout;//pool is always paid
	$actual_payout_disp=$grand_total_no_poolp+$pool_payout;//pool is always paid
	$actual_payout=$grand_total_no_pool+$pool_payout+$undis_pool;//pool is always paid
	$actual_payoutp=$grand_total_no_poolp+$pool_payout+$undis_pool;//pool is always paid
	$actual_payout_percent = $total_volume>0? round($actual_payout/$total_volume*100, 0) : 0;
	$actual_payout_percentp = $total_volume>0? round($actual_payoutp/$total_volume*100, 0) : 0;

	
	
	if($total_volume>0){
		$comm_stats=
		"<p class='bold'>Total Sales: ".number_format($total_volume, 0)."<br />\n
		Projected Payout @ 70%: ".number_format($projected_payout, 0)."<br />\n
		Total Commission: ".number_format($grand_total, 0)." (".number_format(($grand_total/$total_volume*100), 2)."%)"."</p>";
		
		$comm_table=
		"$comm_stats
		<table class='aml_table' style='width:1000px;'>
		 <tr class='aml_header'>
		  <td>No.</td>
		  <td>User ID</td>
		  <td>Acc/No.</td>
		  <td>Country</td>
		  <td>Shares</td>
		  <td>Group Sales</td>
		  <td>Sponsor (".number_format($s_comm_total/$total_volume*100, 2).")</td>
		  <td>Pairing (".number_format($p_comm_total/$total_volume*100, 2).")</td>
		  <td>Leadership (".number_format($l_comm_total/$total_volume*100, 2).")</td>
		  <td>World Pool (".number_format($w_comm_total/$total_volume*100, 2).")</td>
		  <td>Total</td>
		 </tr>
		 $comm_row
		 <tr class='aml_header'>
		  <td colspan='4'></td>
		  <td>".number_format($shares_total, 0)."</td>
		  <td>".number_format($group_sales_network_total, 0)."</td>
		  <td>".number_format($s_comm_total, 0)." (".number_format($s_comm_total/$total_volume*100, 2).")</td>
		  <td>".number_format($p_comm_total, 0)." (".number_format($p_comm_total/$total_volume*100, 2).")</td>
		  <td>".number_format($l_comm_total, 0)." (".number_format($l_comm_total/$total_volume*100, 2).")</td>
		  <td>".number_format($w_comm_total, 0)." (".number_format($w_comm_total/$total_volume*100, 2).")</td>
		  <td>".number_format($grand_total, 0)."</td>
		 </tr>
		</table>";
	}else{
		$comm_stats=
		"<p class='bold'>Total Sales: ".number_format($total_volume, 0)."<br />\n
		Projected Payout @ 70%: ".number_format($projected_payout, 0)."<br />\n
		Total Commission: ".number_format($grand_total, 0)." (".number_format(0, 2)."%)"."</p>";
		
		$comm_table=
		"$comm_stats
		<table class='aml_table' style='width:1000px;'>
		 <tr class='aml_header'>
		  <td>No.</td>
		  <td>User ID</td>
		  <td>Name</td>
		  <td>Country</td>
		  <td>Shares</td>
		  <td>Group Sales</td>
		  <td>Sponsor (".number_format(0, 2).")</td>
		  <td>Pairing (".number_format(0, 2).")</td>
		  <td>Leadership (".number_format(0, 2).")</td>
		  <td>World Pool (".number_format(0, 2).")</td>
		  <td>Total</td>
		 </tr>
		 $comm_row
		 <tr class='aml_header'>
		  <td colspan='4'></td>
		  <td>".number_format($shares_total, 0)."</td>
		  <td>".number_format($group_sales_network_total, 0)."</td>
		  <td>".number_format($s_comm_total, 0)." (".number_format(0, 2).")</td>
		  <td>".number_format($p_comm_total, 0)." (".number_format(0, 2).")</td>
		  <td>".number_format($l_comm_total, 0)." (".number_format(0, 2).")</td>
		  <td>".number_format($w_comm_total, 0)." (".number_format(0, 2).")</td>
		  <td>".number_format($grand_total, 0)."</td>
		 </tr>
		</table>";
	}
}else{
	$use_summary = true;	
	$k=0;
	$count=0;	
	if($use_summary){
		$t=@mysql_num_rows($r=mysql_query($s="select sum(c.pgs) as pgs, sum(c.s) as s, sum(c.p) as p, sum(c.l) as l, sum(c.w) as w, u.name, u.bank_name, u.bank_acc_no, u.country, u.pool_share, c.uid from $db->member_comm_sum c, $db->users u where c.uid=u.id and c.flag=0 group by c.uid"));				
		for($i=0;$i<$t;$i++){			
			$comm=mysql_fetch_assoc($r);				
			//$group_sales_network = @mysql_result(mysql_query("select sum(a.bv) from $db->sales_bv a inner join $db->purchase b on b.id=a.slid and date_format(b.cdate,'%Y-%m-%d') between '".$calc_date['start']."' and '".$calc_date['end']."'"), 0) or 0;				
			$group_sales_network = $comm['pgs'];		
			$s_comm = $comm['s'];				
			$p_comm = $comm['p'];				
			$l_comm = $comm['l'];			
			$w_comm = $comm['w'];
			$share = $comm['pool_share'];					
			$total=$s_comm+$p_comm+$l_comm+$w_comm;				
			$s_comm_total+=$s_comm;				
			$p_comm_total+=$p_comm;				
			$l_comm_total+=$l_comm;				
			$w_comm_total+=$w_comm;			
			$grand_total+=$total;
			$total_share+=$share;
			$group_sales_network_total+=$group_sales_network;	
			$shares_total+=$comm['pool_share'];
			
			//if($total+$group_sales_network<=0) continue;
			
			$comm_row.="<tr class='row$k'><td>".($count+1)."</td><td>$comm[uid]</td><td>$comm[name]</td><td>".$country_code_d[$comm['country']]."</td><td>$comm[pool_share]</td><td>".number_format($group_sales_network, 0)."</td><td>".number_format($s_comm, 0)."</td><td>".number_format($p_comm, 0)."</td><td>".number_format($l_comm, 0)."</td><td>".number_format($w_comm, 0)."</td><td>".number_format($total, 0)."</td></tr>";			
			$k=1-$k;
			$count++;	
		}
	}
	
	//total sales
	//$total_volume=@mysql_result(mysql_query("select sum(a.bv) from $db->sales_bv a inner join $db->purchase b on b.id=a.slid and date_format(b.cdate,'%Y-%m-%d') between '".$calc_date['start']."' and '".$calc_date['end']."'"), 0) or 0;
	$total_volume=mysql_result(mysql_query("select sum(c.pgs) from $db->member_comm_sum c, $db->users u where c.uid=u.id and u.id=10001 group by c.uid"), 0) or 0;
	$projected_payout=round($total_volume * 70 / 100, 0);
	
	$actual_payout_dis=$grand_total_no_pool+$pool_payout;//pool is always paid
	$actual_payout_disp=$grand_total_no_poolp+$pool_payout;//pool is always paid
	$actual_payout=$grand_total_no_pool+$pool_payout+$undis_pool;//pool is always paid
	$actual_payoutp=$grand_total_no_poolp+$pool_payout+$undis_pool;//pool is always paid
	$actual_payout_percent = $total_volume>0? round($actual_payout/$total_volume*100, 0) : 0;
	$actual_payout_percentp = $total_volume>0? round($actual_payoutp/$total_volume*100, 0) : 0;

	$comm_stats=
	"<p class='bold'>Total Sales: ".number_format($total_volume, 0)."<br />\n
	Projected Payout @ 70%: ".number_format($projected_payout, 0)."<br />\n
	Total Commission: ".number_format($grand_total, 0)." (".number_format(($grand_total/$total_volume*100), 2)."%)"."</p>";

	$comm_table=
	"$comm_stats
	<table class='aml_table' style='width:1000px;'>
	 <tr class='aml_header'>
	  <td>No.</td>
	  <td>User ID</td>
	  <td>Name</td>
	  <td>Country</td>
	  <td>Shares</td>
	  <td>Group Sales</td>
	  <td>Sponsor (".number_format($s_comm_total/$total_volume*100, 2).")</td>
	  <td>Pairing (".number_format($p_comm_total/$total_volume*100, 2).")</td>
	  <td>Leadership (".number_format($l_comm_total/$total_volume*100, 2).")</td>
	  <td>World Pool (".number_format($w_comm_total/$total_volume*100, 2).")</td>
	  <td>Total</td>
	 </tr>
	 $comm_row
	 <tr class='aml_header'>
	  <td colspan='4'></td>
	  <td>".number_format($shares_total, 0)."</td>
	  <td>".number_format($group_sales_network_total, 0)."</td>
	  <td>".number_format($s_comm_total, 0)." (".number_format($s_comm_total/$total_volume*100, 2).")</td>
	  <td>".number_format($p_comm_total, 0)." (".number_format($p_comm_total/$total_volume*100, 2).")</td>
	  <td>".number_format($l_comm_total, 0)." (".number_format($l_comm_total/$total_volume*100, 2).")</td>
	  <td>".number_format($w_comm_total, 0)." (".number_format($w_comm_total/$total_volume*100, 2).")</td>
	  <td>".number_format($grand_total, 0)."</td>
	 </tr>
	</table>";
}

$year_start=2014;
$month_start=2;
$year_end=ndate('Y');
$month_end=ndate('n');
$ii=0;
$r_comm_date[$ii++]="all";
$r_comm_date_d["all"]="All";
if($year_end > $year_start){
	for($j=$month_start;$j<=12;$j++){
		$yearmonth=strval($year_start).strval($j);
		$r_comm_date[$ii++]=$yearmonth;
		$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$year_start));
	}
	for($i=$year_start+1;$i<=$year_end;$i++){
		for($j=1;$j<=$month_end;$j++){
			$yearmonth=strval($i).strval($j);
			$r_comm_date[$ii++]=$yearmonth;
			$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$i));
		}
	}
}else{
	for($i=$year_start;$i<=$year_end;$i++){
		for($j=$month_start;$j<=$month_end;$j++){
			$yearmonth=strval($i).strval($j);
			$r_comm_date[$ii++]=$yearmonth;
			$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$i));
		}
	}
}
$comm_date_select=build_select($r_comm_date, $r_comm_date_d, $get_s['comm_date']? $get_s['comm_date'] : 'all', 'comm_date');

//javascript
ob_start();
?>
<link rel="stylesheet" href="<?php echo CSS_URL; ?>/jstheme/ui-lightness/jquery-ui.css" type="text/css" />
<script type="text/javascript" src="<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,fn/fn_urlencode.js"); ?>"></script>
<script type='text/javascript' src='<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,jquery.datepicker.js"); ?>'></script>
<script type="text/javascript">
	jQuery(document).ready(function(j){		
		<?php #datepicker ?>
		j('#date_from').datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			minDate: new Date(2009,5,1), 
			maxDate: '+1M +10D'
		});
		j('#date_to').datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			minDate: new Date(2009,5,1), 
			maxDate: '+1M +10D'
		});
		
		j('input[@name=print_btn]').click(function(){
			window.open('<?php echo addslashes("$this_file?print_comm=1&comm_date="); ?>' + j('select[@name=comm_date] > option:selected').val() + '&uid=' + urlencode(j('input[@name=uid]').val()), 'print_window', 'status=1, toolbar=1, location=1, menubar=1, width=700, height=700');
		});
		j('input[@name=uid]').keypress(function(e){
			if (e.which == 13) {
				j('input[@name=print_btn]').click();
				return false;
			}
		});
		j('input[@name=generate_btn]').click(function(){
			j('input[@name=print_comm]').val('');
			j('input[@name=ep_comm]').val('');
			j('form[@name=comm_form]').submit();
		});
		j('input[@name=ep_btn]').click(function(){
			j('input[@name=print_comm]').val('');
			j('input[@name=ep_comm]').val('1');
			j('form[@name=comm_form]').submit();
		});
		j('form[@name=commsum_form]').submit(function(){
			if(!confirm('<?php echo addslashes(__("Update the bonus remark, are you sure?")); ?>')){
				return false;
			}
		});
	});
</script>
<?php
$jvscript = ob_get_clean();

/*
$comm_form=
"<form name='comm_form' method='get' action='$this_file'>
<input type='hidden' name='__req' value='1' />
<input type='hidden' name='print_comm' />
Commission Date: <input type='text' name='date_from' id='date_from' value=\"".$get_h['date_from']."\" /> to <input type='text' name='date_to' id='date_to' value=\"".$get_h['date_to']."\" /> <input type='button' name='generate_btn' value=\"".__("Generate")."\" /><br />
Print only report for User ID, separate by comma (,), leave blank to print all members: <input type='text' name='uid' /> <input type='button' name='print_btn' value=\"".__("Print Bonus Summary")."\" />
</form>".$msg.$errmsg;
*/

$valid = mysql_result(mysql_query("select count(*) from gg_member_comm where status<>'confirmed' and bonus_date='".$get_h['date_to']."'"), 0) or 0;

if($get_h['date_from']==$get_h['date_to'] && $get_h['date_from']<>"" && $valid>0){
	$comm_form=
	"<form name='comm_form' method='get' action='$this_file'>
	<input type='hidden' name='__req' value='1' />
	<input type='hidden' name='print_comm' />
	<input type='hidden' name='ep_comm' />
	Commission Date: <input type='text' name='date_from' id='date_from' value=\"".$get_h['date_from']."\" /> to <input type='text' name='date_to' id='date_to' value=\"".$get_h['date_to']."\" /> <input type='button' name='generate_btn' value=\"".__("Generate")."\" /><br />
	EP not yet release. To release please click on <input type='button' name='ep_btn' value=\"".__("Release EP")."\" />
	</form>".$msg.$errmsg;
}else{
	$comm_form=
	"<form name='comm_form' method='get' action='$this_file'>
	<input type='hidden' name='__req' value='1' />
	<input type='hidden' name='print_comm' />
	<input type='hidden' name='ep_comm' />
	Commission Date: <input type='text' name='date_from' id='date_from' value=\"".$get_h['date_from']."\" /> to <input type='text' name='date_to' id='date_to' value=\"".$get_h['date_to']."\" /> <input type='button' name='generate_btn' value=\"".__("Generate")."\" /><br />
	</form>".$msg.$errmsg;
}

$content="<h2>$page_title</h2>".$comm_form.$comm_table;

print format_admin_page($content, $this_title, $jvscript);
?>